package graduation.claf.controller;

import graduation.claf.dao.InformationMapper;
import graduation.claf.dao.OwnershipMapper;
import graduation.claf.pojo.Information;
import graduation.claf.pojo.Ownership;
import graduation.claf.tools.ResponseCode;
import graduation.claf.utils.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/portal")
public class InformationController {

    @Autowired
    InformationMapper informationMapper;
    @Autowired
    OwnershipMapper ownershipMapper;

    @RequestMapping(value = "/Information/CheckById.do")
    public ServerResponse checkInformationById(String recipientId){
        List<Information> information = informationMapper.CheckUnreadById(recipientId);
        if (information == null){
            return ServerResponse.createServerResponseByFail(
                    ResponseCode.INFORMATION_NOT_EXISTS.getCode(),
                    ResponseCode.INFORMATION_NOT_EXISTS.getMsg());
        }
        return ServerResponse.createServerResponseBySuccess(information);
    }

    @RequestMapping(value = "/Information/ResponseInformation.do")
    public ServerResponse responseInformation(String informationId,int option){
        Information information = informationMapper.selectByPrimaryKey(informationId);
        switch (information.getType()){
            case 0:{                 //0——处理邀请信息
                information.setIsread(1);
                information.setResult(option);
                try {
                    informationMapper.updateByPrimaryKey(information);
                    String[] param = information.getData().split("\\|");
                    Ownership ownership = new Ownership(param[0],param[1],"member");
                    ownershipMapper.insert(ownership);
                } catch (Exception e){
                    return ServerResponse.createServerResponseByFail(
                            ResponseCode.RESPONSE_FAIL.getCode(),
                            ResponseCode.RESPONSE_FAIL.getMsg());
                }
                return ServerResponse.createServerResponseBySuccess();
            }
            default:
                break;
        }
        return ServerResponse.createServerResponseByFail(
                ResponseCode.PARAMETER_ERROR.getCode(),
                ResponseCode.PARAMETER_ERROR.getMsg());
    }
}
